<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">

    <el-form-item label="供应商名称" prop="splName">
      <el-input v-model="dataForm.splName" placeholder="供应商名称"></el-input>
    </el-form-item>
    <el-form-item label="许可证编号" prop="perNo">
      <el-input v-model="dataForm.perNo" placeholder="许可证编号"></el-input>
    </el-form-item>
    <el-form-item label="经营方式" prop="operateType">
      <el-input v-model="dataForm.operateType" placeholder="经营方式"></el-input>
    </el-form-item>
    <el-form-item label="法人" prop="legalName">
      <el-input v-model="dataForm.legalName" placeholder="法人"></el-input>
    </el-form-item>
    <el-form-item label="负责人" prop="principalName">
      <el-input v-model="dataForm.principalName" placeholder="负责人"></el-input>
    </el-form-item>

    <el-form-item label="住所" prop="perAddr">
      <el-input v-model="dataForm.perAddr" placeholder="住所"></el-input>
    </el-form-item>
    <el-form-item label="经营场所" prop="businessAddr">
      <el-input v-model="dataForm.businessAddr" placeholder="经营场所"></el-input>
    </el-form-item>
    <el-form-item label="库房地址" prop="warehouseAddr">
      <el-input v-model="dataForm.warehouseAddr" placeholder="库房地址"></el-input>
    </el-form-item>
    <el-form-item label="有效期开始日期" prop="beginDate">
      <el-input v-model="dataForm.beginDate" placeholder="有效期开始日期"></el-input>
    </el-form-item>
    <el-form-item label="有效期结束日期" prop="endDate">
      <el-input v-model="dataForm.endDate" placeholder="有效期结束日期"></el-input>
    </el-form-item>
    <el-form-item label="经营范围" prop="businessScope">
      <el-input v-model="dataForm.businessScope" placeholder="经营范围"></el-input>
    </el-form-item>
    <el-form-item label="发证部门" prop="regOrg">
      <el-input v-model="dataForm.regOrg" placeholder="发证部门"></el-input>
    </el-form-item>
    <el-form-item label="发证日期" prop="regDate">
      <el-input v-model="dataForm.regDate" placeholder="发证日期"></el-input>
    </el-form-item>


    <el-form-item label="备注信息" prop="remarks">
      <el-input v-model="dataForm.remarks" placeholder="备注信息"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          perCode: 0,
          splCode: '',
          splName: '',
          perNo: '',
          operateType: '',
          legalName: '',
          principalName: '',
          qmName: '',
          perAddr: '',
          businessAddr: '',
          warehouseAddr: '',
          beginDate: '',
          endDate: '',
          businessScope: '',
          regOrg: '',
          regDate: '',
          status: '',
          createBy: '',
          createDate: '',
          updateBy: '',
          updateDate: '',
          remarks: ''
        },
        dataRule: {
          splName: [
            { required: true, message: '供应商名称不能为空', trigger: 'blur' }
          ],
          perNo: [
            { required: true, message: '许可证编号不能为空', trigger: 'blur' }
          ],
          operateType: [
            { required: true, message: '经营方式不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.perCode = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.perCode) {
            this.$http({
              url: this.$http.adornUrl(`/basedata/supplierper/info/${this.dataForm.perCode}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.splCode = data.supplierPer.splCode
                this.dataForm.splName = data.supplierPer.splName
                this.dataForm.perNo = data.supplierPer.perNo
                this.dataForm.operateType = data.supplierPer.operateType
                this.dataForm.legalName = data.supplierPer.legalName
                this.dataForm.principalName = data.supplierPer.principalName
                this.dataForm.qmName = data.supplierPer.qmName
                this.dataForm.perAddr = data.supplierPer.perAddr
                this.dataForm.businessAddr = data.supplierPer.businessAddr
                this.dataForm.warehouseAddr = data.supplierPer.warehouseAddr
                this.dataForm.beginDate = data.supplierPer.beginDate
                this.dataForm.endDate = data.supplierPer.endDate
                this.dataForm.businessScope = data.supplierPer.businessScope
                this.dataForm.regOrg = data.supplierPer.regOrg
                this.dataForm.regDate = data.supplierPer.regDate
                this.dataForm.status = data.supplierPer.status
                this.dataForm.createBy = data.supplierPer.createBy
                this.dataForm.createDate = data.supplierPer.createDate
                this.dataForm.updateBy = data.supplierPer.updateBy
                this.dataForm.updateDate = data.supplierPer.updateDate
                this.dataForm.remarks = data.supplierPer.remarks
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/basedata/supplierper/${!this.dataForm.perCode ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'perCode': this.dataForm.perCode || undefined,
                'splCode': this.dataForm.splCode,
                'splName': this.dataForm.splName,
                'perNo': this.dataForm.perNo,
                'operateType': this.dataForm.operateType,
                'legalName': this.dataForm.legalName,
                'principalName': this.dataForm.principalName,
                'qmName': this.dataForm.qmName,
                'perAddr': this.dataForm.perAddr,
                'businessAddr': this.dataForm.businessAddr,
                'warehouseAddr': this.dataForm.warehouseAddr,
                'beginDate': this.dataForm.beginDate,
                'endDate': this.dataForm.endDate,
                'businessScope': this.dataForm.businessScope,
                'regOrg': this.dataForm.regOrg,
                'regDate': this.dataForm.regDate,
                'status': this.dataForm.status,
                'createBy': this.dataForm.createBy,
                'createDate': this.dataForm.createDate,
                'updateBy': this.dataForm.updateBy,
                'updateDate': this.dataForm.updateDate,
                'remarks': this.dataForm.remarks
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
